/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.screen;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.screen.WeddingScreenDao;
import com.boxin.ims.modules.wechat.entity.screen.WeddingScreen;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 婚庆微信墙Service
 * @author Jakemanse
 * @version 2013-11-01
 */
@Component
@Transactional(readOnly = true)
public class WeddingScreenService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(WeddingScreenService.class);
	
	@Autowired
	private WeddingScreenDao weddingScreenDao;
	
	public WeddingScreen get(Long id) {
		return weddingScreenDao.findOne(id);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-1  下午11:17:38
	 * @function <p>查找当前用户 当天的结婚对象   </p>
	 * @param userId
	 * @param weddingDay
	 * @return
	 */
	public List<WeddingScreen> findByUserIdAndDate(Long userId,String weddingDay){
		DetachedCriteria dc = weddingScreenDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("weddingDay", weddingDay));
		
		return weddingScreenDao.find( dc);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-2  下午4:49:35
	 * @function <p> 均分页查询 </p>
	 * @param weddingScreen
	 * @return
	 */
	public List<WeddingScreen> find(WeddingScreen weddingScreen){
		DetachedCriteria dc = weddingScreenDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", weddingScreen.getUser().getId()));
		dc.add(Restrictions.eq("weddingDay", weddingScreen.getWeddingDay()));
		
		return weddingScreenDao.find( dc);
	}
	
	public Page<WeddingScreen> find(Page<WeddingScreen> page, WeddingScreen weddingScreen) {
		DetachedCriteria dc = weddingScreenDao.createDetachedCriteria();
		if (weddingScreen.getUser()!=null && weddingScreen.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", weddingScreen.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(weddingScreen.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+weddingScreen.getRemarks()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return weddingScreenDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(WeddingScreen weddingScreen) {
		weddingScreenDao.save(weddingScreen);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		weddingScreenDao.deleteById(id);
	}
	
}
